package com.corgi.test.java8.time;

import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

/**
 * @title: NewTime
 * @description:
 * @author: dengmiao
 * @create: 2019-11-15 11:26
 **/
@Slf4j
public class NewLocalDate {

    public static void main(String[] args) {
        // 获取当前系统时钟下的日期
        LocalDate now = LocalDate.now();
        log.info("现在是.now() =>: {}", now);

        // 时间反格式化
        LocalDate ofNow = LocalDate.of(2019, 11, 15);
        LocalDate parseNow = LocalDate.parse("2019-11-15");
        log.info("现在是.of() => {}, .parse() => {}", ofNow, parseNow);

        // 日期加减
        // 加一天
        LocalDate tomorrow = now.plusDays(1);
        // 加一周
        LocalDate nextWeekOfDay = now.plusWeeks(1);
        // 加一个月
        LocalDate nextMonthOfDay = now.plusMonths(1);
        // 加一年
        LocalDate nextYearOfDay = now.plusYears(1);
        // enum
        LocalDate enumPlus = now.plus(1, ChronoUnit.DAYS);
        // 减一个月
        LocalDate previousMonthSameDay = LocalDate.now().minus(1, ChronoUnit.MONTHS);
        log.info("明天是: {} {}, 下周的今天是: {}, 下个月的今天是: {}, 明年的今天是: {}, 上个月的今天是: {}",
                tomorrow, enumPlus, nextWeekOfDay, nextMonthOfDay, nextYearOfDay, previousMonthSameDay);

        // 获取日期时间单位 某天是当周 当月 当年的第多少天
        int dayOfMonth = now.getDayOfMonth(), dayOfYear = now.getDayOfYear();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        log.info("今天是: {}, 今天是当月的第: {}天, 今天是当年的第: {}天", dayOfWeek, dayOfMonth, dayOfYear);

        // 某年是不是闰年
        boolean leapYear = now/*.plusYears(1)*/.isLeapYear();
        log.info("今年{}是闰年", leapYear ? "" : "不");

        // 判断日期先后
        boolean notBefore = now.isBefore(LocalDate.parse("2019-11-16"));
        boolean isAfter = now.isAfter(LocalDate.parse("2019-11-14"));
        boolean before = LocalDate.parse("2019-11-15").isBefore(LocalDate.parse("2019-11-15"));
        boolean after = LocalDate.parse("2019-11-15").isAfter(LocalDate.parse("2019-11-15"));
        log.info("{}, {}, {}, {}", notBefore, isAfter, before, after);

        // 日期边界
        LocalDateTime beginningOfDay = now.atStartOfDay();
        LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
        log.info("今天的开始时间是: {}, 当月的第一天是: {}", beginningOfDay, firstDayOfMonth);
    }
}
